package com.jlastu.znb.view;

import android.util.Log;
import android.widget.EditText;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.jlastu.znb.R;
import com.jlastu.znb.adapter.GoodsListAdapter;
import com.jlastu.znb.base.BaseFragment;
import com.jlastu.znb.contract.MainContract;
import com.jlastu.znb.entity.Goods;
import com.jlastu.znb.presenter.MainPresenter;

import java.util.ArrayList;
import java.util.List;

public class FirstFragment extends BaseFragment implements MainContract.IMainView {

   private RecyclerView mGoodsRecyclerView;
   private EditText mSearchEdit;
   private GoodsListAdapter mRecyclerAdapter;
   private MainContract.IMainPresenter mMainPresenter;
   private List<Goods> mGoodsList;

   @Override
   protected int getLayoutId() {
      return R.layout.fragment_home;
   }

   @Override
   protected void initView() {
      mMainPresenter = new MainPresenter(this, getActivity());
      mGoodsList = new ArrayList<>();
      mSearchEdit = mView.findViewById(R.id.search_edit);
      mGoodsRecyclerView = mView.findViewById(R.id.rv_goods);
      StaggeredGridLayoutManager staggeredGridLayoutManager =
              new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
      mGoodsRecyclerView.setLayoutManager(staggeredGridLayoutManager);
      mRecyclerAdapter = new GoodsListAdapter(mGoodsList,getActivity());
      mGoodsRecyclerView.setAdapter(mRecyclerAdapter);

   }

   @Override
   public void onResume() {
      super.onResume();
      mMainPresenter.getGoodsList();
   }

   @Override
   protected void initListener() {

   }

   @Override
   public void showGoodsList(List<Goods> goodsList) {
      mGoodsList = goodsList;
      Log.d("", "showGoodsList " + mGoodsList.size());
      mRecyclerAdapter.updateData(mGoodsList);
   }
}
